package com.yanqu.road.web.controller.usercenter.cn.activity;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.impl.servercenter.DbInfoDaoImpl;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.servercenter.DbInfo;
import com.yanqu.road.entity.web.cn.activity.CnActivityGetUserScoreParam;
import com.yanqu.road.logic.bussiness.cn.activity.CnActivityBussiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.helper.SignHelper;
import com.yanqu.road.server.manger.AppKeyMgr;
import com.yanqu.road.server.manger.CnActivityMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.web.mgr.AllActivityMainInfoMgr;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

@RestController
public class CnActivityGetUserScore {

    private static Logger logger = LogManager.getLogger(CnActivityGetUserScore.class.getName());

    @RequestMapping("/getCnActivityUserScore")
    public Object getCnActivityUserScore(@RequestBody CnActivityGetUserScoreParam param, HttpServletRequest request) {
        JSONObject result = new JSONObject();
        result.put("state", 1);
        result.put("data", new Object());
        result.put("msg", "success");

        SortedMap<String,String> paramMap = new TreeMap<>();
        paramMap.put("platformUserId",param.getPlatformUserId());
        paramMap.put("type",String.valueOf(param.getType()));
        paramMap.put("activityTime",String.valueOf(param.getActivityTime()));

        if(!SignHelper.checkSign(paramMap, AppKeyMgr.getAndroidAppSecret(), param.getSign())) {
            result.put("msg", "sign校验失败");
            result.put("state", 0);
            logger.info("sign 校验失败");
            return result;
        }

        if (!eActivityType.isCnActivityType(param.getType())) {
            result.put("msg", "活动类型不支持");
            result.put("state", 0);
            logger.info("活动类型不支持");
            return result;
        }

        // 通过报名表获取对应的服务器ID
        long serverId = CnActivityMgr.getServerIdByPlatformUserId(param.getPlatformUserId());
        if (serverId == 0) {
            result.put("msg", "玩家未报名");
            result.put("state", 0);
            logger.info("玩家未报名");
            return result;
        }

        List<Integer> types = new ArrayList<>();
        types.add(param.getType());
        List<Integer> activityIdList = AllActivityMainInfoMgr.getActivityIdListByServerIdAndType((int) serverId, types, param.getActivityTime());

        // 有活动
        if (activityIdList.size() > 0) {
            ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityIdList.get(0));
            // 获取条件类型
            int conditionType = eActivityType.getCnActivityRankConditionType(activityInfo.getType());
            DbInfo gameServerDbInfo = new DbInfoDaoImpl().getServerDbInfo((int) serverId).get(0);
            String gameDbParam = gameServerDbInfo.getIp() + "|" + gameServerDbInfo.getPort() + "|" + gameServerDbInfo.getDbName() + "|" + gameServerDbInfo.getUserName() + "|" + gameServerDbInfo.getPassword();

            JSONObject object = CnActivityBussiness.getActivityScore(gameDbParam, param.getPlatformUserId(), activityInfo.getActivityId(), conditionType);
            object.put("platformUserId", param.getPlatformUserId());
            object.put("serverName", ServerListMgr.getServerInfo(serverId).getServerName());
            object.put("serverId", serverId);

            result.put("data", object);
            return result;
        }

        result.put("msg", "没有活动");
        result.put("state", 0);
        return result;
    }

    public static void main(String[] args) {

    }

}

